package grimbo.goodwinfooty.server.mvc;

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;

/**
 * Test datastore save.
 */
public class DatastoreSaveController extends AbstractController {
	private static final String CNAME = DatastoreSaveController.class.getName();
	private static final Logger log = Logger.getLogger(CNAME);

	private DatastoreService datastoreService;

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		final String MNAME = "handleRequestInternal(HttpServletRequest, HttpServletResponse)";
		final boolean DEBUG = log.isLoggable(Level.FINE);

		if (DEBUG) {
			log.entering(CNAME, MNAME);
		}

		String kind = req.getParameter("kind");
		Entity entity = new Entity(kind);
		Key key = datastoreService.put(entity);

		if (DEBUG) {
			log.exiting(CNAME, MNAME);
		}

		return new ModelAndView("datastore-save-results", "results", key);
	}

	public DatastoreService getDatastoreService() {
		return datastoreService;
	}

	public void setDatastoreService(DatastoreService datastoreService) {
		this.datastoreService = datastoreService;
	}
}
